﻿using LyProduct.Modules.PartyBuilding.Entities;
//using LyProduct.Modules.PartyBuilding.PersonManage.Entitys.SemesterSchedule;
using Microsoft.EntityFrameworkCore.Internal;
using Namotion.Reflection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using T4Console.Dto;
using T4Console.Service;

namespace T4Console
{
    public class Program
    {
        static void Main(string[] args)
        {
            var typeList = new List<Type>()
            {
                typeof(Reservation)
            };

            var generateTemplateList = new List<GenerateTemplate>();

            var ignoreList = new List<string>()
            {
               "IsDeleted",
               "DeleterUserId",
               "DeletionTime",
               "LastModificationTime",
               "LastModifierUserId",
               "CreationTime",
               "CreatorUserId",
               "TenantId",
            };

            foreach (var type in typeList)
            {
                var generateTemplateFieldList = new List<GenerateTemplateField>();
                foreach (PropertyInfo propertyInfo in type.GetProperties())
                {
                    if (!ignoreList.Contains(propertyInfo.Name))
                    {
                        string fieldTypeName = string.Empty;
                        string hasColumnTypeName = string.Empty;
                        if (propertyInfo.PropertyType.IsValueType)
                        {
                            if (propertyInfo.PropertyType.IsGenericType)
                            {
                                (string propertyFieldName, string columnTypeName) = ConversionTypeName(propertyInfo.PropertyType.GenericTypeArguments[0].Name);
                                fieldTypeName = propertyFieldName + "?";
                                hasColumnTypeName = columnTypeName;
                            }
                            else
                            {
                                (string propertyFieldName, string fieldTypeConfigName) = ConversionTypeName(propertyInfo.PropertyType.Name);
                                fieldTypeName = propertyFieldName;
                                hasColumnTypeName = fieldTypeConfigName;
                            }
                        }
                        else
                        {
                            if (propertyInfo.PropertyType == typeof(String))
                            {
                                fieldTypeName = nameof(String).ToLower();
                                hasColumnTypeName = ".HasColumnType(\"varchar(200)\")";
                            }
                            else
                            {
                                fieldTypeName = propertyInfo.PropertyType.Name;
                            }

                        }
                        var generateTemplateField = new GenerateTemplateField(propertyInfo.Name, fieldTypeName, propertyInfo.GetXmlDocsSummary(), hasColumnTypeName);
                        generateTemplateFieldList.Add(generateTemplateField);
                    }
                }

                var generateTemplate = new GenerateTemplate(type.Name,
                    type.GetXmlDocsSummary(), generateTemplateFieldList);

                generateTemplateList.Add(generateTemplate);
            }

            foreach (var generateTemplate in generateTemplateList)
            {
                // 获取当前目录
                string currentDirectory = @"D:\study\T4Sample\T4Console\GenerateFile\";

                // 在当前目录下创建新的文件夹
                string newDirectory = generateTemplate.ClassName;
                if (!Directory.Exists(currentDirectory + newDirectory))
                {
                    Directory.CreateDirectory(currentDirectory + newDirectory);
                }

                var url = currentDirectory + "/" + newDirectory + "/";

                DtoTemplate page = new DtoTemplate(generateTemplate);
                String pageContent = page.TransformText();
                string filePath = $"{url}{generateTemplate.ClassName}Dto.cs";
                // 写入Dto文件
                File.WriteAllText(filePath, pageContent);

                ServiceTemplate serviceTemplate = new ServiceTemplate(generateTemplate);
                String serviceTemplateContent = serviceTemplate.TransformText();
                string serviceTemplateFilePath = $"{url}{generateTemplate.ClassName}Service.cs";
                // 写入Service文件
                File.WriteAllText(serviceTemplateFilePath, serviceTemplateContent);
            }

            Console.WriteLine("文件写入成功！");
            Console.ReadKey();
        }

        /// <summary>
        /// 转换类型名称
        /// </summary>
        public static (string, string) ConversionTypeName(string fieldTypeName)
        {
            var typeName = string.Empty;
            var columnTypeName = string.Empty;
            switch (fieldTypeName)
            {
                case "Guid":
                    typeName = "Guid";
                    break;
                case "Int32":
                    typeName = "int";
                    break;
                case "String":
                    typeName = "string";
                    columnTypeName = ".HasColumnType(\"varchar(200)\")";
                    break;
                case "Double":
                    typeName = "double";
                    break;
                case "Decimal":
                    typeName = "decimal";
                    columnTypeName = ".HasColumnType(\"decimal(8,2)\")";
                    break;
                case "Int64":
                    typeName = "long";
                    break;
                case "Boolean":
                    typeName = "bool";
                    break;
                case "DateTime":
                    typeName = "DateTime";
                    break;
                default:
                    typeName = fieldTypeName;
                    break;
            }

            return (typeName, columnTypeName);
        }

    }

}
